import { ref } from "vue";
import { deleteAdvertAPI, getAdvertListAPI } from "@/api/resource.ts";
import { ElMessage } from "element-plus";

export default function useAdvertising() {
  const dialog = ref()
  const advertisingList = ref([])

  // 获取广告列表
  const getAdvertisingList = async () => {
    const res: any = await getAdvertListAPI()
    if(res.code === 0) {
      advertisingList.value = res.data
    }
  }

  // 添加广告
  const addAdvertising = () => {
    dialog.value.init({}, 0)
  }

  // 编辑广告
  const editAdvertising = (row: any) => {
    dialog.value.init(row, 1)
  }

  // 删除广告
  const deleteAdvertising = async (id: string) => {
    const res: any = await deleteAdvertAPI(id)
    ElMessage({
      type: res.code === 0 ? 'success' : 'error',
      message: res.message
    })
    await getAdvertisingList()
  }

  return {
    dialog,
    advertisingList,
    getAdvertisingList,
    addAdvertising,
    editAdvertising,
    deleteAdvertising
  }
}
